home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / util / cdity / ModeProSrc.lha / MPFiles.c < prev    next >
C/C++ Source or Header  |  1998-08-29  |  8KB  |  235 lines

  1. #include "MPFiles.h"
  2.  
  3. //extern UWORD   defaultpens[];
  4. extern WORD    NumDriPens;
  5. extern struct MPSem *MPSem;
  6.  
  7. void ReadChunk(struct ParseChunk *PC,APTR Dest,ULONG Size);
  8. STRPTR ReadChunkString(struct ParseChunk *PC);
  9.  
  10. struct ParseChunk
  11. {
  12.   UBYTE *Data;
  13. };
  14.  
  15.  
  16. BOOL LoadFile(struct List *List, UBYTE *fname)
  17. {   
  18.        LONG bdsize;
  19.                                   UBYTE *bddatastart;
  20.   struct IFFHandle *iff;
  21.   struct CollectionItem *ci,*cip;
  22.   struct StoredProperty *sp;
  23.   LONG error=0; 
  24.   struct ParseChunk pc;
  25.   struct DefaultNode *n;
  26.   struct Promo pm;
  27.   LONG numpens,pen;
  28.   UWORD font; // Don't Change Type
  29.   ULONG flags;
  30.  
  31.   if(iff=AllocIFF())
  32.   { 
  33.     if(iff->iff_Stream = Open (fname,MODE_OLDFILE))
  34.     {
  35.       InitIFFasDOS(iff);
  36.       if(!(error=OpenIFF(iff,IFFF_READ)) )
  37.       {
  38.         if (!(error=CollectionChunk(iff,ID_PREF,ID_PRMO)))
  39.         {
  40.           if (!(error=PropChunk(iff,ID_PREF,ID_MPPR)))
  41.           {
  42.             if(!(error=StopOnExit(iff,ID_PREF,ID_FORM)))           
  43.             {
  44.               if((error=ParseIFF(iff,IFFPARSE_SCAN))>-3)
  45.               {
  46.                 error=0;
  47.                 
  48.                 if(sp=FindProp(iff,ID_PREF,ID_MPPR))
  49.                 {
  50.                   struct MPPR_Chunk *mppr;
  51.  
  52.                   mppr=(struct MPPR_Chunk *)sp->sp_Data;
  53.                   MPSem->Enabled      =mppr->EnablePromotion;
  54.                   MPSem->EnabledNLM   =mppr->EnableNLM;
  55.                   MPSem->CenterAll    =mppr->CenterUnknown;
  56.                   MPSem->CatchScreens =mppr->AskUnknown;
  57.                   if(mppr->Version>0)
  58.                     MPSem->CatchPubScreens =mppr->AskUnknownPub;
  59.                 }
  60.                 
  61.                 ci=FindCollection(iff,ID_PREF,ID_PRMO);
  62.                 cip=ci;
  63.                 while(cip!=NULL && error>=0)
  64.                 {
  65.                   if(n=AllocDefaultNode(0))
  66.                   {
  67.                     pc.Data=(UBYTE *)cip->ci_Data;
  68.                     ReadChunk(&pc,&pm,sizeof(struct Promo));                  
  69.                     n->Type         =pm.Type;
  70.                     n->ModeSelect   =pm.ModeSelect;
  71.                     n->ModeID       =pm.ModeID;
  72.                     n->Width        =pm.Width;
  73.                     n->Height       =pm.Height;
  74.                     n->OverscanType =pm.OverscanType;
  75.                     n->AutoScroll   =pm.AutoScroll;
  76.                     n->Look3D       =pm.Look3D;
  77.                     n->Def_Node.ln_Name=ReadChunkString(&pc);
  78.   
  79.                     if(pm.Version>0)
  80.                     {
  81.                       ReadChunk(&pc,&numpens,sizeof(numpens));
  82.                       ReadChunk(&pc,&n->Pens[0] ,sizeof(UWORD)*min(DRIPENS,numpens));
  83.                       for(pen=numpens;pen<DRIPENS;pen++)
  84.                         n->Pens[pen]=0;//defaultpens[pen];
  85.                       numpens-=DRIPENS;
  86.                       if(numpens>0)
  87.                       {
  88.                         ReadChunk(&pc,0                  ,sizeof(UWORD)*numpens);
  89.                       }
  90.                       if(pm.Version>1 && !error)
  91.                       {
  92.                         ReadChunk(&pc,&flags,sizeof(flags));
  93.                         n->Flags=flags;
  94.                         
  95.                         if(n->Flags & CENTER)
  96.                         {
  97.                           n->Flags&=(~CENTER); // clear obsolete flag.
  98.                           n->CenterFlags = 3;  // set new flag.
  99.                         }
  100.                         ReadChunk(&pc,&font,sizeof(font));
  101.                         n->FontType=font;
  102.                         
  103.                         if(n->FontType==SFONT_MP)
  104.                         {
  105.                           n->Font.tta_Name=ReadChunkString(&pc);
  106.                           ReadChunk(&pc,&n->Font.tta_YSize,4);//<-'4'=TextAttr struct less ta_Name
  107.                         }
  108.                         
  109.                         if(pm.Version>2 && !error)
  110.                         {
  111.                           ReadChunk(&pc,&n->PubOptions,4);
  112.                           n->PubName=ReadChunkString(&pc);
  113.                             
  114.                           if(pm.Version>3 && !error)
  115.                           {
  116.                             ReadChunk(&pc,&n->Depth,2);
  117.                             n->LockedPens=ReadChunkString(&pc);
  118.                             if(pm.Version>4 && !error)
  119.                             { // Version 5
  120.                               n->HotKey=ReadChunkString(&pc);
  121.                             }
  122.                             if(pm.Version>5 && !error)
  123.                             { // Version 6
  124.                               ReadChunk(&pc,&n->OrigDisplayID,sizeof(ULONG));
  125.                             }
  126.                             if(pm.Version>6 && !error)
  127.                             { // Version 7
  128.                               ReadChunk(&pc,&n->ODFlags,sizeof(UBYTE)*2);
  129.                               ReadChunk(&pc,&n->Colors,sizeof(ULONG));
  130.                               if(n->Colors)
  131.                               {
  132.                                 if(n->Palette=AllocVec(sizeof(struct RGB)*n->Colors,MEMF_PUBLIC))
  133.                                 {
  134.                                   ReadChunk(&pc,n->Palette,sizeof(struct RGB)*n->Colors);
  135.                                 }
  136.                                 else
  137.                                   error=TRUE;
  138.                               }
  139.                               if(pm.Version>7 && !error)
  140.                               { // Version 8
  141.                                 ReadChunk(&pc,&n->BackdropType, sizeof(UWORD)); // loads BackdropFlags aswell
  142.                                 if(!n->BackdropType) // handle old versions of file
  143.                                   n->BackdropType=(n->BackdropFlags & BF_ON ? 1:0);
  144.                                 //n->BackdropFlags&=~BF_ON;
  145.                                 n->BackdropFile=ReadChunkString(&pc);
  146.                                 if(pm.Version>8 && !error)
  147.                                 { // Version 9
  148.                            
  149.                                   ReadChunk(&pc,&bdsize, sizeof(bdsize));
  150.                                   bddatastart=pc.Data;
  151.                                   
  152.                                   switch(n->BackdropType)
  153.                                   {
  154.                                     case 2: //RGB
  155.                                       ReadChunk(&pc,&n->BackdropRGB, sizeof(struct RGB));
  156.                                       bdsize-=sizeof(struct RGB);
  157.                                       break;
  158.                                   }
  159.                                     
  160.                                   // set Data ptr to right place 
  161.                                   pc.Data=bddatastart+bdsize;
  162.                                 }
  163.                               }
  164.                             }
  165.                           }
  166.                         }
  167.                       }
  168.                     }
  169.                     else
  170.                       for(pen=0;pen<NumDriPens;pen++)
  171.                         n->Pens[pen]=0;//defaultpens[pen];
  172.                     
  173.                     if(error)
  174.                       FreeDNode(n);
  175.                     else
  176.                       if(pm.Type<NUM_LISTS)
  177.                         AddHead(&List[pm.Type],(struct Node *)n);
  178.                       else
  179.                         FreeDNode(n);      
  180.                   }
  181.                   else
  182.                     error=-1;
  183.                  cip=cip->ci_Next;
  184.                 }/* end while */
  185.               }/* end ParseIFF if */        
  186.             }
  187.           } /* End Prop MPPR */
  188.         }/* End Collect PRMO */  
  189.         CloseIFF(iff);
  190.       }
  191.       Close(iff->iff_Stream);
  192.     }
  193.     FreeIFF(iff);
  194.   }
  195.   
  196.   if(error<0)
  197.     return(FALSE);
  198.   else
  199.     return(TRUE);
  200. }
  201.  
  202. void ReadChunk(struct ParseChunk *PC,APTR Dest,ULONG Size)
  203. {
  204.   if(PC)
  205.   {
  206.     if(Dest && PC->Data && Size)
  207.     {
  208.       CopyMem((APTR)PC->Data,(APTR)Dest,Size);
  209.     }
  210.     PC->Data+=Size;
  211.   }
  212. }
  213.  
  214. STRPTR ReadChunkString(struct ParseChunk *PC)
  215. {
  216.   STRPTR str=0;
  217.   ULONG size;
  218.   
  219.   if(PC)
  220.   {
  221.     size=strlen(PC->Data)+1;
  222.     str=AllocVec(size,MEMF_PUBLIC|MEMF_CLEAR);
  223.  
  224.     if(PC->Data && size)
  225.       CopyMem((APTR)PC->Data,(APTR)str,size);
  226.  
  227.     if(size & 1)
  228.       size++;
  229.     
  230.     PC->Data+=size;
  231.   }
  232.   return(str);
  233. }
  234.  
  235.